home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_02 / rtiff.c < prev    next >
C/C++ Source or Header  |  1992-02-27  |  6KB  |  220 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.        /**************************************************
  8.        *
  9.        *       file d:\cips\rtiff.c
  10.        *
  11.        *       Functions: This file contains
  12.        *          read_tiff_image
  13.        *          read_line
  14.        *          seek_to_first_line
  15.        *          seek_to_end_of_line
  16.        *
  17.        *       Purpose:
  18.        *  These functions read a TIFF image and insert
  19.        *  the data into a ROWSxCOLS array of short.
  20.        *
  21.        *
  22.        *       External Calls:
  23.        *  mof.c - my_open
  24.        *  mrw.c - my_read
  25.        *  tiff.c - read_tiff_header
  26.        *
  27.        *       Modifications:
  28.        *       25 June 1990 - created
  29.        *
  30.        ******************************************************/
  31.  
  32.  
  33. #include "d:\cips\cips.h"
  34.  
  35.  
  36.  
  37.  
  38.  
  39. read_tiff_image(image_file_name, array, il, ie, ll, le)
  40.  
  41.       char   image_file_name[];
  42.       int    il, ie, ll, le;
  43.         short   array[ROWS][COLS];
  44. {
  45.    char  buffer[COLS],
  46.        rep[80];
  47.    int bytes_read,
  48.        closed,
  49.        file_descriptor,
  50.        i;
  51.    float a;
  52.    long  line_length, offset;
  53.  
  54.    unsigned long position;
  55.  
  56.    struct tiff_header_struct image_header;
  57.  
  58.    read_tiff_header(image_file_name, &image_header);
  59.  
  60.       /****************************************************
  61.       *
  62.       *   Procedure:
  63.       *   Seek to the strip offset where the data begins.
  64.       *   Seek to the first line you want.
  65.       *   Loop over the lines you want to read:
  66.       *      Seek to the first element of the line.
  67.       *      Read the line.
  68.       *      Seek to the end of the data in that line.
  69.       *
  70.       ****************************************************/
  71.  
  72.    /*file_descriptor = my_open(image_file_name);*/
  73.    file_descriptor = open(image_file_name, O_BINARY | O_RDONLY);
  74.    /*printf("\nRTIFF.C> file desc = %d", file_descriptor);*/
  75.    if(file_descriptor > 0){
  76.  
  77.    position     = lseek(file_descriptor, image_header.strip_offset, 0);
  78.    position     = seek_to_first_line(file_descriptor, &image_header, il);
  79.  
  80.    for(i=0; i<(ll-il); i++){
  81.       offset     = (ie-1)/(8/image_header.bits_per_pixel);
  82.       position     = lseek(file_descriptor, offset, 1);
  83.       bytes_read = read_line(file_descriptor, array, i, &image_header, ie, le);
  84.       position     = seek_to_end_of_line(file_descriptor, le, &image_header);
  85.       position     = lseek(file_descriptor, 1, 1); /*???*/
  86.    }  /* ends loop over i  */
  87.  
  88.    closed = close(file_descriptor);
  89.    }  /* ends if file opened ok */
  90.    else{
  91.       printf("\nRTIFF.C> ERROR - cannot open tiff file");
  92.    }
  93.  
  94. }  /*  ends read_tiff_image */
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.        /*******************************************************
  102.        *
  103.        *   read_line(...
  104.        *
  105.        *   This function reads bytes from the TIFF file into
  106.        *   a buffer, extracts the numbers from that buffer,
  107.        *   and puts them into a ROWSxCOLS array of shorts.
  108.        *   The process depends on the number of bits per
  109.        *   pixel used in the file (4 or 8).
  110.        *
  111.        *******************************************************/
  112.  
  113. read_line(file_descriptor, array, line_number, image_header, ie, le)
  114.    int  file_descriptor, ie, le, line_number;
  115.    short  array[ROWS][COLS];
  116.    struct tiff_header_struct *image_header;
  117. {
  118.    char  buffer[COLS], first, second;
  119.    float a, b;
  120.    int bytes_read, i;
  121.    unsigned int bytes_to_read;
  122.    union short_char_union scu;
  123.  
  124.    for(i=0; i<COLS; i++)
  125.       buffer[i] = '\0';
  126.  
  127.         /**********************************************
  128.         *
  129.         *   Use the number of bits per pixel to calculate
  130.         *   how many bytes to read.
  131.         *
  132.         *************************************************/
  133.  
  134.    bytes_to_read = (le-ie)/(8/image_header->bits_per_pixel);
  135.    bytes_read    = read(file_descriptor, buffer, bytes_to_read);
  136.  
  137.    for(i=0; i<bytes_read; i++){
  138.  
  139.         /**********************************************
  140.         *
  141.         *   Use unions defined in cips.h to stuff bytes
  142.         *   into shorts.
  143.         *
  144.         *************************************************/
  145.  
  146.       if(image_header->bits_per_pixel == 8){
  147.        scu.s_num          = 0;
  148.        scu.s_alpha[0]        = buffer[i];
  149.        array[line_number][i] = scu.s_num;
  150.       }  /* ends if bits_per_pixel == 8 */
  151.  
  152.       if(image_header->bits_per_pixel == 4){
  153.  
  154.        scu.s_num             = 0;
  155.        second                       = buffer[i] & 0X000F;
  156.        scu.s_alpha[0]        = second;
  157.        array[line_number][i*2+1] = scu.s_num;
  158.  
  159.        scu.s_num             = 0;
  160.        first                 = buffer[i] >> 4;
  161.        first                 = first & 0x000F;
  162.        scu.s_alpha[0]        = first;
  163.        array[line_number][i*2] = scu.s_num;
  164.  
  165.       }  /* ends if bits_per_pixel == 4 */
  166.  
  167.    }  /*  ends loop over i  */
  168.  
  169.    return(bytes_read);
  170.  
  171. }  /* ends read_line  */
  172.  
  173.  
  174.        /*******************************************************
  175.        *
  176.        *   seek_to_first_line(...
  177.        *
  178.        *******************************************************/
  179.  
  180. seek_to_first_line(file_descriptor, image_header, il)
  181.    int  file_descriptor, il;
  182.    struct tiff_header_struct *image_header;
  183. {
  184.    long offset;
  185.    unsigned long position;
  186.  
  187.    offset   = (il-1)*image_header->image_width/
  188.              (8/image_header->bits_per_pixel);
  189.       /* seek from current position */
  190.    position = lseek(file_descriptor, offset, 1);
  191.    return(position);
  192. }  /* ends seek_to_first_line */
  193.  
  194.  
  195.  
  196.  
  197.        /*******************************************************
  198.        *
  199.        *   seek_to_end_of_line(...
  200.        *
  201.        *******************************************************/
  202.  
  203. seek_to_end_of_line(file_descriptor, le, image_header)
  204.    int file_descriptor, le;
  205.    struct tiff_header_struct *image_header;
  206. {
  207.    int origin;
  208.    long  offset;
  209.    unsigned long position;
  210.  
  211.    offset   = (image_header->image_width-le)/
  212.              (8/image_header->bits_per_pixel);
  213.    origin   = 1;     /* seek from the current position     */
  214.    position = lseek(file_descriptor, offset, origin);
  215.    return(position);
  216. }  /* ends seek_to_end_of_line         */
  217.  
  218.  
  219.  
  220.